package com.situ.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.situ.mybatis.entity.SearchCondition;
import com.situ.mybatis.entity.Student;
import com.situ.mybatis.entity.User;
import com.situ.mybatis.mapper.IStudentDao;
import com.situ.mybatis.mapper.IUserDao;
import com.situ.mybatis.util.myBatisUtil;

public class MyBatisTest2 {
	
	@Test
	public void testSelectById() throws IOException {
		// 加载核心配置文件
//		String resource = "mybatis.xml";
//		InputStream inputStream = Resources.getResourceAsStream(resource);
//		// 创建SqlSessionFactory
//		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
//		// 创建SqlSession
//		SqlSession sqlSession = factory.openSession();
		
		
		SqlSession sqlSession = myBatisUtil.getSqlSession();
		// 执行sql语句
		User user = sqlSession.selectOne("user.selectById",4);
		System.out.println(user);
		
	}
	
	@Test
	public void testSelectAll() {
		SqlSession sqlSession = myBatisUtil.getSqlSession();
		List<User>list = sqlSession.selectList("user.selectAll");
		for (User user : list) {
			System.out.println(user);
		}
	}
	
	@Test
	public void testInsert() {
		SqlSession sqlSession = myBatisUtil.getSqlSession();
		User user = new User();
		user.setName("李三");
		user.setPassword("234");
		int count = sqlSession.update("user.insert",user);
		System.out.println(count);
		// 对于更新类的操作需要手动提交
		sqlSession.commit();
		sqlSession.close();
	}
	
	@Test
	public void testDeleteById() {
		SqlSession sqlSession = myBatisUtil.getSqlSession();
		int count = sqlSession.update("user.deleteById",4);
		System.out.println(count);
		// 对于更新类的操作需要手动提交
		sqlSession.commit();
		sqlSession.close();
	}
	
	@Test
	public void testUpdate() {
		SqlSession sqlSession = myBatisUtil.getSqlSession();
		User user = new User();
		user.setId(1);
		user.setName("张三");
		user.setPassword("123");
		int count = sqlSession.update("user.update",user);
		System.out.println(count);
		//对于更新类的操作需要手动提交
		sqlSession.commit();
		sqlSession.close();
	}
	
	@Test
	public void testSelectBySearchCondition() {
		SqlSession sqlSession = myBatisUtil.getSqlSession();
		User user = new User();
		user.setName("admin");
		SearchCondition searchCondition = new SearchCondition();
		searchCondition.setUser(user);
		List<User>list =sqlSession.selectList("user.selectBySearchCondition",searchCondition);
		for (User u : list) {
			System.out.println(u);
		}
	}
		@Test
		public void testPageList() {
			SqlSession sqlSession = myBatisUtil.getSqlSession();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("offset", 1);
			map.put("pageSize", 2);
			List<User> list = sqlSession.selectList("user.pageList", map);
			for (User user : list) {
				System.out.println(user);
			}
			
	}
		@Test
		public void testCount() {
			SqlSession sqlSession = myBatisUtil.getSqlSession();
			int count = sqlSession.selectOne("user.count");
			System.out.println(count);
		}
		
		@Test
		public void test11() {
			SqlSession sqlSession = myBatisUtil.getSqlSession();
			// IUserDao userDao = new UserDaoImpl();
			// 动态代理开发方式，由MyBatis帮助我们去生成了一个IUserDao的实现类
			IUserDao userDao = sqlSession.getMapper(IUserDao.class);
			List<User>list = userDao.selectAll();
			for (User user : list) {
				System.out.println(user);
			}
		}
		
		@Test
		public void test1() {
			SqlSession sqlSession = myBatisUtil.getSqlSession();
			IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
			Student student = new Student();
			
			student.setName("王五");
			//student.setAge(34);
			List<Student>list = studentDao.search(student);
			for (Student stu : list) {
				System.out.println(stu);
			}
		}
		
		@Test
		public void test2() {
			SqlSession sqlSession = myBatisUtil.getSqlSession();
			IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
			Student student = new Student();
			
			student.setId(20);
			//student.setName("张三");
			student.setAge(99);
			student.setGender("男");
			int count = studentDao.update(student);
			sqlSession.commit();
			System.out.println(count);
		}
		
		@Test
		public void selectByIdArray() {
			SqlSession sqlSession = myBatisUtil.getSqlSession();
			IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
			int[] idArray = {23,24,26};
			List<Student>list = studentDao.selectByIdArray(idArray);
			for (Student students : list) {
				System.out.println(students);
			}
		}
		
		
		@Test
		public void selectByIdList() {
			SqlSession sqlSession = myBatisUtil.getSqlSession();
			IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
			List<Integer>idList = new ArrayList<Integer>();
			idList.add(23);
			idList.add(24);
			idList.add(26);
			List<Student>list = studentDao.selectByIdList(idList);
			for (Student student : list) {
				System.out.println(student);
			}
		}
		
		
		@Test
		public void searchByCondition() {
			SqlSession sqlSession = myBatisUtil.getSqlSession();
			IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
			Student student = new Student();
			student.setAge(20);
			List<Student>list = studentDao.searchByCondition(student);
			for (Student stu : list) {
				System.out.println(stu);
			}
		}
}
